/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) 
	{
		if(pHead1==nullptr||pHead2 == nullptr)
		{
			return nullptr;
		}
		//首先确定两个链表是否存在相交的节点，并且确定各自的长度
		int len1 = 1;
		int len2 = 1;
		ListNode* cur1 = pHead1;
		ListNode*  cur2 = pHead2;
		while(cur1->next!=nullptr) 
		{
			cur1 = cur1->next;
			len1++;
		}   
		while(cur2->next!=nullptr)
		{
			cur2 = cur2->next;
			len2++;
		}
		if(cur1!=cur2)
		{
			return nullptr;//不存在相交的节点
		}
		ListNode* longlist = len1>=len2?pHead1:pHead2;
		ListNode* shortlist = len1<len2?pHead1:pHead2;
		int k = (len1>len2?len1-len2:len2-len1);
		//让长链表先走k步
		while(k--)
		{
			longlist = longlist->next;
		}
		while(longlist!=shortlist)
		{
			longlist= longlist->next;
			shortlist = shortlist->next;
		}
		return shortlist;
    }
};
